<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="/socket.io/socket.io.js"></script>
        <script>
            var socket = io.connect();
            socket.on('login', function (names) {
                var str = "";
                names.forEach(function (name) {
                    str += "User" + name + "already login. <br/>";
                });
                document.getElementById('result').innerHTML = str;
            });

            function add() {
                socket.emit('login', document.getElementById('nickname').value);
            }

            socket.on('message', function(data) {
                console.log(data);
                socket.send('message accept');
            });
            socket.on('disconnect', function() {
                console.log('server disconnect');
            });
            socket.on('news', function (data) {
                console.log(data.hello);
                socket.emit('my other event', {my: 'data'});
            });
            socket.on('setName', function (name, fn) {
                console.log(name);
                fn('leon1', 'leon2');
            });

            function setNickName() {
                socket.emit('set nickname', document.getElementById('nickname').value);
            }
            function getNickName() {
                socket.emit('get nickname');
                socket.on('send nickname', function (data) {
                    console.log('My nickname is: ' + data);
                });
                socket.on('err', function (data) {
                    console.log('server error: ' + data);
                });
            }
        </script>

        <script>
            var chat = io.connect('http://localhost:1337/chat');
            var news = io.connect('http://localhost:1337/news');
            chat.on('connect', function () {
                chat.send('Hello.');
                chat.on('message', function(msg) {
                    console.log('From chat namespace accept message: ', msg);
                });
            });
            news.on('connect', function() {
                news.emit('send message', 'hello.');
                news.on('send message', function(data) {
                    console.log('From news namespace accept send message event, data is:', data);
                });
            });

        </script>
    </head>
    <body>
        <!--[if lt IE 7]>
            <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->
        Username: <input type="text" id="nickname" />
        <input type="button" onclick="add()" value="login"/></br>
        <output id="result" />
        <!--<input type="button" onclick="setNickName()" value="Set Nickname"/>
        <input type="button" onclick="getNickName()" value="Get Nickname"/>-->
    </body>
</html>